package com.phrp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.phrp.entity.HouseRecord;
import com.phrp.util.returnResult.ResponseResult;
import com.phrp.dao.HouseRecordDao;
import com.phrp.entity.dto.ApplyPassDataDTO;
import com.phrp.service.HouseRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.phrp.util.returnResult.ReturnInfoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ssc
 * @since 2024-05-05
 */
@Service
public class HouseRecordServiceImpl extends ServiceImpl<HouseRecordDao, HouseRecord> implements HouseRecordService {

    @Autowired
    private HouseRecordDao houseRecordDao;

    /**
     * 获取申请通过的数据
     * @return
     */
    @Override
    public ResponseResult getApplyPassData(String type) {
        // 获取时间，上周一周的时间间隔
        LocalDate now = LocalDate.now();
        // 计算上周周一
        LocalDate lastMonday = now.with(TemporalAdjusters.previous(DayOfWeek.MONDAY))
                .minusWeeks(1);

        // 计算上周周日
        LocalDate lastSunday = now.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY));

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        // 获取数据
        List<ApplyPassDataDTO> houseRecords = houseRecordDao.selectApplyPass(lastMonday.format(formatter),lastSunday.format(formatter),type);

        return new ResponseResult(ReturnInfoEnum.SUCCESS,houseRecords);
    }

    /**
     * 根据身份证搜索内容
     * @param identity
     * @return
     */
    @Override
    public ResponseResult searchData(String identity) {
        ApplyPassDataDTO data = houseRecordDao.selectByIdentity(identity);
        return new ResponseResult(ReturnInfoEnum.SUCCESS,data);
    }
}
